package com.tl.commonlibrary.ui.exception;

import android.content.ClipboardManager;

import com.tl.commonlibrary.AppConfig;
import com.tl.commonlibrary.storage.configuration.PrefUtil;
import com.tl.commonlibrary.storage.filecache.ACache;
import com.tl.commonlibrary.tool.DeviceUtil;
import com.tl.commonlibrary.tool.Logger;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/7/27 0027.
 */

public class CrashHandler implements Thread.UncaughtExceptionHandler {
    public static final String KEY_CRASH = "kcsh";
    private static final CrashHandler mCrashHandler = new CrashHandler();
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    private CrashHandler() {
    }

    public static CrashHandler get() {
        return mCrashHandler;
    }

    public void init() {
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {
//        将日志信息复制到剪切板
//        ClipboardManager manager = (ClipboardManager) AppConfig.appContext.getSystemService(AppConfig.appContext.CLIPBOARD_SERVICE);
//        manager.setText(e.getMessage());
        if (AppConfig.DEBUG()) {
//            PrefUtil prefUtil = new PrefUtil(AppConfig.appContext,KEY_CRASH);
            StringBuilder sb = new StringBuilder();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
            sb.append("Time:    ").append(format.format(new Date(System.currentTimeMillis()))).append("\r\n\r\n");
            sb.append("Model:    ").append(AppConfig.appModel).append("\r\n\r\n");
            sb.append("VersionName:    ").append(AppConfig.appVersion).append("\r\n\r\n");
            sb.append("VersionCode:    ").append(AppConfig.appVersionCode).append("\r\n\r\n");
            sb.append("SdkVersion:    ").append(DeviceUtil.getSdkVersionCode() + "-" + DeviceUtil.getSdkVersionName()).append("\r\n\r\n");
            sb.append("Crash Start  =============>").append("\r\n\r\n");
            Throwable throwable = e.getCause();
            Writer writer = new StringWriter();
            PrintWriter printWriter = new PrintWriter(writer);
            if (throwable == null) {
                printWriter.print(e.toString());
            } else {
                while (throwable != null) {
                    throwable.printStackTrace(printWriter);
                    printWriter.print("\n");
                    throwable = throwable.getCause();
                }
            }
            printWriter.close();
            sb.append(writer.toString());
            try {
                writer.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            sb.append("\r\n\r\nCrash End <=============");
            ACache.get(AppConfig.appContext, KEY_CRASH).put(KEY_CRASH + System.currentTimeMillis(), sb.toString());
            mDefaultHandler.uncaughtException(t, e);
        } else {
            mDefaultHandler.uncaughtException(t, e);
        }
    }
}
